<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: HCZ <huangchengze@etrol.com>
// +----------------------------------------------------------------------

namespace app\alarm\controller\api;
use app\common\controller\Api;

class Alarm extends Api {
	
	/**
	 * 获取警报
	 * @param string $id
	 * @return Json
	 */
	public function getAlarms($id = null){
		$where = [];
		if($id){
			$where['id'] = $id;
		}
		$info = collection(model('Alarm')->where($where)->select())->toArray();
		return json(['code'=>1,'msg'=>'','count'=>count($info),'data'=>$info]);
	}
	
	/**
	 * 更新警报
	 * @param array $data
	 * @return Json
	 */
	public function alarmHandling($data = null){
		$model = model('Alarm');
		if (empty($data)) {
			return json(['code'=>0,'msg'=>'数据异常,请检查']);
		}
		$result = $model->change($data);
		if($result !== false){
			if(model('Sensor')->where('id',$data['sensorId'])->update(['status'=>1])){
				return json(['code'=>1,'msg'=>'警报处理成功']);
			}
		}
		return json(['code'=>0,'msg'=>'更改失败,请检查']);
	}
	
	
	public function edit($data = null){
		$model = model('Alarm');
		if (empty($data)) {
			return json(['code'=>0,'msg'=>'数据异常,请检查']);
		}
		$result = $model->change($data);
		if($result !== false){
			setlog('Alarmupdate','Alarm',session('user_auth.id'));
			return json(['code'=>1,'msg'=>'更改成功']);
			
		}
		return json(['code'=>0,'msg'=>'更改失败,请检查']);
	}
}